package com.yuke.cloud.service.uac.controller.frontend;

import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.core.annotation.LogAnnotation;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.uac.exceptions.UacBizException;
import com.yuke.cloud.service.uac.dto.BindUserRolesDto;
import com.yuke.cloud.service.uac.dto.RoleBindUserDto;
import com.yuke.cloud.service.uac.dto.RoleBindUserReqDto;
import com.yuke.cloud.service.uac.entity.AccUserRole;
import com.yuke.cloud.service.uac.service.AccUserRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.service.uac.vo.UserBindRoleVo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
* Created by wg on 2018/09/15.
*/
@Api(value = "WEB - AccUserRoleController", tags = "处理AccUserRole相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/acc/user_role")
public class AccUserRoleController extends BaseController {
    @Resource
    private AccUserRoleService accUserRoleService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "accUserRole", value = "该数据记录的POJO") @RequestBody AccUserRole accUserRole) {
        accUserRoleService.save(accUserRole);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        accUserRoleService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "accUserRole", value = "需要修改的内容") @RequestBody AccUserRole accUserRole) {
        accUserRoleService.update(accUserRole);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<AccUserRole> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        AccUserRole accUserRole = accUserRoleService.findById(id);
        return WrapMapper.ok(accUserRole);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<AccUserRole>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<AccUserRole> list = accUserRoleService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 获取角色绑定用户页面数据.
     *
     * @param roleId the role id
     *
     * @return the wrapper
     */
    @GetMapping(value = "/getBindUser/{roleId}")
    @ApiOperation(httpMethod = "GET", value = "获取角色绑定用户页面数据")
    public Wrapper<RoleBindUserDto> getBindUser(@ApiParam(name = "roleId", value = "角色id") @PathVariable Long roleId) {
        logger.info("获取角色绑定用户页面数据. roleId={}", roleId);
        LoginAuthDto loginAuthDto = super.getLoginAuthDto();
        Long currentUserId = loginAuthDto.getUserId();
        RoleBindUserDto bindUserDto = accUserRoleService.getRoleBindUserDto(roleId, currentUserId);
        return WrapMapper.ok(bindUserDto);
    }

    /**
     * 角色绑定用户.
     *
     * @param roleBindUserReqDto the role bind user req dto
     *
     * @return the wrapper
     */
    @LogAnnotation
    @PostMapping(value = "/bindUser")
    @ApiOperation(httpMethod = "POST", value = "角色绑定用户")
    public Wrapper bindUser(@ApiParam(name = "uacRoleBindUserReqDto", value = "角色绑定用户") @RequestBody RoleBindUserReqDto roleBindUserReqDto) {
        logger.info("roleBindUser={}", roleBindUserReqDto);
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        accUserRoleService.bindUser4Role(roleBindUserReqDto, loginAuthDto);
        return WrapMapper.ok();
    }

    /**
     * 获取用户绑定角色页面数据.
     *
     * @param userId the user id
     *
     * @return the bind role
     */
    @GetMapping(value = "/getBindRole/{userId}")
    @ApiOperation(httpMethod = "GET", value = "获取用户绑定角色页面数据")
    public Wrapper<UserBindRoleVo> getBindRole(@ApiParam(name = "userId", value = "角色id") @PathVariable Long userId) {
        logger.info("获取用户绑定角色页面数据. userId={}", userId);
        LoginAuthDto loginAuthDto = super.getLoginAuthDto();
        Long currentUserId = loginAuthDto.getUserId();
        if (Objects.equals(userId, currentUserId)) {
            throw new UacBizException(ErrorCodeEnum.UAC10011023);
        }

        UserBindRoleVo bindUserDto = accUserRoleService.getUserBindRoleDto(userId);
        return WrapMapper.ok(bindUserDto);
    }

    /**
     * 用户绑定角色.
     *
     * @param bindUserRolesDto the bind user roles dto
     *
     * @return the wrapper
     */
    @PostMapping(value = "/bindRole")
    @LogAnnotation
    @ApiOperation(httpMethod = "POST", value = "用户绑定角色")
    public Wrapper<Integer> bindUserRoles(@ApiParam(name = "bindUserRolesDto", value = "用户绑定角色Dto") @RequestBody BindUserRolesDto bindUserRolesDto) {
        logger.info("用户绑定角色 bindUserRolesDto={}", bindUserRolesDto);
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        accUserRoleService.bindUserRoles(bindUserRolesDto, loginAuthDto);
        return WrapMapper.ok();
    }
}
